import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
import axiosUtil from '../axios-util/axios-util';
import Login from "../components/Login.vue";
import Main from "../components/Main.vue";
import Son from "../components/Son.vue";
import AiAppBg from "../components/pages/AiAppBg.vue";
import AiVisionBg from "../components/pages/AiVisionBg.vue";
import AiVisionBgPro from "../components/pages/AiVisionBgPro.vue";
import BatchImageBg from "../components/pages/BatchImageBg.vue";
import BatchVideoBg from "../components/pages/BatchVideoBgNew.vue";
import CollageBg from "../components/pages/CollageBg.vue";
import DeepseekBg from "../components/pages/DeepseekBg.vue";
import DigitalHumanBg from "../components/pages/DigitalHumanBg.vue";
import Hotspot from "../components/pages/Hotspot.vue";
import InternetTraffic from "../components/pages/InternetTraffic.vue";
import Matrix from "../components/pages/Matrix.vue";
import Laboratory from "../components/pages/Laboratory.vue";
import VideoWarehouse from "../components/pages/VideoWarehouse.vue";
import Image2Video from "../components/pages/Image2Video.vue";
import Image2VideoPro from "../components/pages/Image2VideoPro.vue";
import Text2Video from "../components/pages/Text2Video.vue";
import Text2VideoPro from "../components/pages/Text2VideoPro.vue";
import GetTxtFromMaterial from "../components/pages/GetTxtFromMaterial.vue";
import DataStatistics from "../components/pages/DataStatistics.vue";
import LocalStorageUtil from "../utils/LocalStorageUtil";
import type { RouteRecordRaw } from 'vue-router';
import utils from '../utils/utils';

const rs: RouteRecordRaw[] = [   //定义路由表
    {
        path: '/login',
        component: Login,
        name: '登录',
        meta: {
            // 允许匿名访问
            permitAnony: true
        },
    },
    {
        path: '/',
        redirect: (to) => {
            return { name: '首页' };
        },
        name: '重定向',
    },
    {
        path: '/ai',
        component: Main,
        name: '首页',
    },
    {
        path: '/son',
        component: Son,
        name: '二级页面',
    },
    {
        path: '/aiAppBg',
        component: AiAppBg,
        name: 'AiPPT',
    },
    {
        path: '/aiVisionBg',
        component: AiVisionBg,
        name: '文生图',
    },
    {
        path: '/aiVisionBgPro',
        component: AiVisionBgPro,
        name: '文生图Pro',
    },
    {
        path: '/batchImageBg',
        component: BatchImageBg,
        name: '批量制图',
    },
    {
        path: '/batchVideoBg',
        component: BatchVideoBg,
        name: '批量成片',
    },
    {
        path: '/collageBg',
        component: CollageBg,
        name: '一键拼图',
    },
    {
        path: '/deepseekBg',
        component: DeepseekBg,
        name: 'DeepSeek火山版',
    },
    {
        path: '/digitalHumanBg',
        component: DigitalHumanBg,
        name: '数字人',
    },
    {
        path: '/hotspot',
        component: Hotspot,
        name: '热点洞察',
    },
    {
        path: '/internetTraffic',
        component: InternetTraffic,
        name: '流量洞察',
    },
    {
        path: '/laboratory',
        component: Laboratory,
        name: '多功能实验室',
    },
    {
        path: '/matrix',
        component: Matrix,
        name: '矩阵营销',
    },
    {
        path: '/videoWarehouse',
        component: VideoWarehouse,
        name: '视频库',
    },
    {
        path: '/image2Video',
        component: Image2Video,
        name: '图生视频',
    },
    {
        path: '/image2VideoPro',
        component: Image2VideoPro,
        name: '图生视频Pro',
    },
    {
        path: '/text2Video',
        component: Text2Video,
        name: '文生视频',
    },
    {
        path: '/text2VideoPro',
        component: Text2VideoPro,
        name: '文生视频Pro',
    },
    {
        path: '/getTxtFromMaterial',
        component: GetTxtFromMaterial,
        name: '提取文案',
    },
    {
        path: '/dataStatistics',
        component: DataStatistics,
        name: '数据统计',
    },
]

//定义路由器
const router = createRouter({
    routes: rs,
    history: createWebHashHistory("/storeadmin/")
    // history: createWebHistory(),
})

// 注册全局前置守卫
router.beforeEach((to, from, next) => {
    if (to.path !== '/login' && to.path !== '/' && to.path !== '/ai' && to.path !== '') {
        // 检查是否已存在相同路径的路由，先移除
        utils.globalCacheMenu = utils.globalCacheMenu.filter(route => route.path !== to.path);
        utils.globalCacheMenu.push({
            path: to.path,
            name: to.name,
        })
        // 最多缓冲7个路由，超出移除左侧的
        if (utils.globalCacheMenu.length > 7) {
            utils.globalCacheMenu.shift();
        }
    }

    if (to.meta && to.meta.permitAnony) {
        // 允许匿名访问
        next();
    } else {
        // 需要登录，检查用户是否已登录
        const token = LocalStorageUtil.get('token')
        if (!token) {
            // 用户未登录，重定向到登录页
            next('/login');
            return;
        }
        // 用户已登录,放行
        next();
    }
});

//导出路由器
export default router
